{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "yTbIyn5Hsob0"
      },
      "outputs": [],
      "source": [
        "# This script scrapes the results of training and generates numbers in the\n",
        "# format of Tables 1 and 2 in the paper (https://arxiv.org/abs/2103.13415).\n",
        "# Numbers are slightly different because of implementation details and\n",
        "# randomness across different runs of training."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "wbSGA8PNKvIy"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import numpy as np\n",
        "from google3.pyglib import gfile"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gKnbxt2AKz8w"
      },
      "outputs": [],
      "source": [
        "blender_scenes = ['chair', 'drums', 'ficus', 'hotdog', 'lego', 'materials', 'mic', 'ship']\n",
        "\n",
        "def summarize_results(folder, scene_names, num_buckets):\n",
        "  metric_names = ['psnrs', 'ssims', 'lpips']\n",
        "  num_iters = 1000000\n",
        "  precisions = [3, 4, 4, 4]\n",
        "\n",
        "  results = []\n",
        "  for scene_name in scene_names:\n",
        "    test_preds_folder = os.path.join(folder, scene_name, 'test_preds')\n",
        "    values = []\n",
        "    for metric_name in metric_names:\n",
        "      filename = os.path.join(folder, scene_name, 'test_preds', f'{metric_name}_{num_iters}.txt')\n",
        "      with gfile.Open(filename) as f:\n",
        "        v = np.array([float(s) for s in f.readline().split(' ')])\n",
        "        values.append(np.mean(np.reshape(v, [-1, num_buckets]), 0))\n",
        "    results.append(np.concatenate(values))\n",
        "  avg_results = np.mean(np.array(results), 0)\n",
        "\n",
        "  psnr, ssim, lpips = np.mean(np.reshape(avg_results, [-1, num_buckets]), 1)\n",
        "\n",
        "  mse = np.exp(-0.1 * np.log(10.) * psnr)\n",
        "  dssim = np.sqrt(1 - ssim)\n",
        "  avg_avg = np.exp(np.mean(np.log(np.array([mse, dssim, lpips]))))\n",
        "\n",
        "  s = []\n",
        "  for i, v in enumerate(np.reshape(avg_results, [-1, num_buckets])):\n",
        "    s.append(' '.join([f'{s:0.{precisions[i]}f}' for s in v]))\n",
        "  s.append(f'{avg_avg:0.{precisions[-1]}f}')\n",
        "  return ' | '.join(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "executionInfo": {
          "elapsed": 10288,
          "status": "ok",
          "timestamp": 1619902906249,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "YxsRO0c5kC7F",
        "outputId": "92e7f5e7-ef8b-4694-a89d-321748934bd7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "mip-NeRF: 32.634 34.350 35.505 35.636 | 0.9578 0.9703 0.9786 0.9834 | 0.0469 0.0260 0.0168 0.0120 | 0.0114\n"
          ]
        }
      ],
      "source": [
        "# The Multiscale Blender benchmark.\n",
        "# These numbers roughly correspond to the \"Mip-NeRF\" row of Table 1.\n",
        "print('mip-NeRF: ' + summarize_results('/cns/oz-d/home/barron/nerf/mipnerf/multiblender', blender_scenes, 4))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "executionInfo": {
          "elapsed": 8608,
          "status": "ok",
          "timestamp": 1619902914863,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "00fXHEbBpNPR",
        "outputId": "8f31319d-b724-43b2-c27b-cf4d9cc99d0b"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "mip-NeRF: 33.085 | 0.9605 | 0.0425 | 0.0161\n"
          ]
        }
      ],
      "source": [
        "# The (single-scale) Blender benchmark.\n",
        "# These numbers roughly correspond to the \"Mip-NeRF\" row of Table 2.\n",
        "print('mip-NeRF: ' + summarize_results('/cns/oz-d/home/barron/nerf/mipnerf/blender', blender_scenes, 1))"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "last_runtime": {
        "build_target": "//learning/deepmind/public/tools/ml_python:ml_notebook",
        "kind": "private"
      },
      "name": "summarize.ipynb",
      "provenance": [
        {
          "file_id": "/piper/depot/google3/experimental/users/barron/prob_nerf/scripts/eval_multi.ipynb?workspaceId=barron:mipnerf_loglin::citc",
          "timestamp": 1618455902794
        },
        {
          "file_id": "/piper/depot/google3/experimental/users/barron/prob_nerf/scripts/Pre_NeRF_Eval.ipynb?workspaceId=barron:jaxnerf_mono5::citc",
          "timestamp": 1614038274387
        },
        {
          "file_id": "10opVizeODokMJ10R7hwq7qVyLmYZx_ZA",
          "timestamp": 1613166364224
        }
      ]
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
